var yearDataMap=null;
var monthDataMap=null;
var yearTimeLinelabels=[];
//十二个月
var monthTimeLinelabels=['1','2','3','4','5','6','7','8','9','10','11','12'];

$.getJSON("/rest/fault/getFaultDatasByGroupYearRankType",function(result){
		yearDataMap = result;
		for(var i in yearDataMap){
			yearTimeLinelabels.push(yearDataMap[i].name);
		}
		$.getJSON("/rest/fault/getFaultDatasByGroupMonthRankType",function(result){
			monthDataMap = result;
			initdata();
		});
	});

//通过年份相同返回年数据对象
var findyeardata = function(y) {
	for ( var i in yearDataMap) {
		if (y == yearDataMap[i].name) {			
				return yearDataMap[i];
			
		}
	}
};
//通过年和月返回数据对象
var findYMdata=function(y,m){
	for(var i in monthDataMap){
		if(y==monthDataMap[i].name){
			for(var k in monthDataMap[i].value){
				if(m==monthDataMap[i].value[k].name){
					return monthDataMap[i].value[k];
				}				
			}
		}
	 
	}
};
//通过年返回月的数据对象
var findmonthdata=function(y){
	for(var i in monthDataMap){
		if(y==monthDataMap[i].name){		
				return monthDataMap[i].value;			
		}
	 
	}
};
//根据月份确定data对象
var getDatas=function(y){
	var monthdata=findmonthdata(y);
	var dataarray=[];
	for(var k in monthdata){
		var xDataValue={
				name:'单位',
				value:[]
		};
		var yDataValue={
				name:'数量',
				value:[]
		};
		var getDeviceCountArray = function(dname) {
			var values = yDataValue.value;
			for ( var i in values) {
				var yvalue = values[i];
				if (yvalue.lineName == dname) {
					return yvalue;
				}
			}
			return null;
		};
		
		if (monthdata[k]) {
			for ( var i in monthdata[k].value) {
				var areaobj = monthdata[k].value[i];
				xDataValue.value.push(areaobj.name);
				var areaobjvalues = areaobj.value;
				for ( var m in areaobjvalues) {
					var dfobj = areaobjvalues[m];
					var lineYdata = getDeviceCountArray(dfobj.name);
					if (lineYdata) {
						lineYdata.lineValue.push(dfobj.value);
					} else {
						lineYdata = {
							lineName : dfobj.name,
							lineValue : [ dfobj.value ]
						};
						yDataValue.value.push(lineYdata);
					}
				}
			}
		}

		dataarray.push({xData : xDataValue,yData : yDataValue});
	}
	return dataarray;
	
};

//根据年份确定data对象
var getLineData = function(y) {
	var yeardata = findyeardata(y);
	var xDataValue = {
		name : '单位',
		value : []
	};
	var yDataValue = {
		name : '数量',
		value : []
	};
	var getDeviceCountArray = function(dname) {
		var values = yDataValue.value;
		for ( var i in values) {
			var yvalue = values[i];
			if (yvalue.lineName == dname) {
				return yvalue;
			}
		}
		return null;
	};
	
	if (yeardata) {
		for ( var k in yeardata.value) {
			var areaobj = yeardata.value[k];
			xDataValue.value.push(areaobj.name);
			var areaobjvalues = areaobj.value;
			for ( var j in areaobjvalues) {
				var dfobj = areaobjvalues[j];
				var lineYdata = getDeviceCountArray(dfobj.name);
				if (lineYdata) {
					lineYdata.lineValue.push(dfobj.value);
				} else {
					lineYdata = {
						lineName : dfobj.name,
						lineValue : [ dfobj.value ]
					};
				yDataValue.value.push(lineYdata);
				}
			}
		}
	}
	return {
		xData : xDataValue,
		yData : yDataValue
	};
};
//根据年份和月份确定data对象
var getMonthData=function(y,m){
	var monthdata=findYMdata(y,m);
	var xDataValue={
			name:'单位',
			value:[]
	};
	var yDataValue={
			name:'数量',
			value:[]
	};
	var getDeviceCountArray = function(dname) {
		var values = yDataValue.value;
		for ( var i in values) {
			var yvalue = values[i];
			if (yvalue.lineName == dname) {
				return yvalue;
			}
		}
		return null;
	};
	
	if (monthdata) {
		for ( var i in monthdata.value) {
			var areaobj = monthdata.value[i];
			xDataValue.value.push(areaobj.name);
			var areaobjvalues = areaobj.value;
			for ( var m in areaobjvalues) {
				var dfobj = areaobjvalues[m];
				var lineYdata = getDeviceCountArray(dfobj.name);
				if (lineYdata) {
					lineYdata.lineValue.push(dfobj.value);
				} else {
					lineYdata = {
						lineName : dfobj.name,
						lineValue : [ dfobj.value ]
					};
					yDataValue.value.push(lineYdata);
				}
			}
		}
	}
	return {
		xData : xDataValue,
		yData : yDataValue
	};
};

var monthdarray=null;
//初始化数据
function initdata() {

	    //时间轴
	    var timeBars = function(chartid) {		
		var barschart = echarts.init(document.getElementById(chartid));
		var option = {
			timeline : {
				// y: 0,
				axisType : 'category',
				// realtime: false,
				// loop: false,
				autoPlay : false,
				 currentIndex: 0,
				playInterval : 1000,
				// controlStyle: {
				// position: 'left'
				// },
				data :[]
				
			}
		};
	    this.getOption=function(){
	    	return option;
	    };
		this.show = function() {
			barschart.setOption(option);
			return this;
		};
		this.getChart = function() {
			return barschart;
		};
	};
	var yearsTimeBar = new timeBars("yearsline");
	var munthsTimeBar = new timeBars("monthsline");
	var yearBars = new CategoryBar("yearBars");
	var monthBars = new CategoryBar("monthBars");
	yearsTimeBar.getOption().timeline.data=yearTimeLinelabels;
	munthsTimeBar.getOption().timeline.data=monthTimeLinelabels;
	munthsTimeBar.getOption().timeline.autoPlay=false;
	yearsTimeBar.show();
	munthsTimeBar.show();
	var startyear=yearTimeLinelabels[0];
	var endyear=yearTimeLinelabels[yearTimeLinelabels.length-1];
	$("#yearsid").append(startyear+"年~"+endyear+"年南方电网500kV系统一次设备故障年统计表");
	//默认显示第一年
	var linedata = getLineData(startyear);
	yearBars.setData(linedata);
	yearBars.show();
	monthdarray=findmonthdata(startyear);		
	for(var i in monthdarray){
		var month=monthdarray[i].name;
		var md=getMData(currentyear,month);  
		monthBars.setData(md);			
	}	
	monthBars.show();
	if($("#yearid").empty()){
		$("#yearid").append(startyear+"年南方电网500kV系统一次设备故障月统计表");
	}else{
		$("#yearid").empty();
	}
	var currentyear ='';
	   //年轴改变事件
	    yearsTimeBar.getChart().on("timelinechanged", function(param1) {
		currentyear = yearTimeLinelabels[param1.currentIndex];
		// yearBars = new CategoryBar("yearBars");
		var linedata = getLineData(currentyear);
		yearBars.setData(linedata);
		yearBars.show();
		//根据年联动月的变化
		monthdarray=findmonthdata(currentyear);		
		for(var i in monthdarray){
			var month=monthdarray[i].name;
			var md=getMData(currentyear,month);  
			monthBars.setData(md);			
		}		
		monthBars.show();
		if($("#yearid").empty()){
			$("#yearid").append(currentyear+"年南方电网500kV系统一次设备故障月统计表");
		}else{
			$("#yearid").empty();
		}
  });

    //月轴改变事件
    munthsTimeBar.getChart().on("timelinechanged", function(param2) {
    if(currentyear==''){
    	currentyear=startyear;
    }
    var month = monthTimeLinelabels[param2.currentIndex];
	var monthdatas=getMonthData(currentyear,month);  
	monthBars.setData(monthdatas);
	monthBars.show();
   });
    //动态改变宽度
    var t1 = window.setInterval(function() {
    	var width = $('#boxid').width() + "";
    	if (width != '0') {		
    		$("#yearsline").css("width", width);
    		$("#yearBars").css("width", width);
    		$("#monthsline").css("width", width);
    		$("#yearBars").css("width", width);
    		yearBars.getChart().resize();
    		monthBars.getChart().resize();
    		window.clearInterval(t1);
    	}
     }, 20);
}
var finddata=function(y,m){
	for(var i in monthdarray){		
				if(m==monthdarray[i].name){
					return monthdarray[i];
				}				
			}			 	
};

var getMData=function(y,m){
	var monthdata=finddata(y,m);
	var xDataValue = {
			name : '单位',
			value : []
		};
		var yDataValue = {
			name : '数量',
			value : []
		};
		var getDeviceCountArray = function(dname) {
			var values = yDataValue.value;
			for ( var i in values) {
				var yvalue = values[i];
				if (yvalue.lineName == dname) {
					return yvalue;
				}
			}
			return null;
		};
		
		if (monthdata) {
			for ( var k in monthdata.value) {
				var areaobj = monthdata.value[k];
				xDataValue.value.push(areaobj.name);
				var areaobjvalues = areaobj.value;
				for ( var j in areaobjvalues) {
					var dfobj = areaobjvalues[j];
					var lineYdata = getDeviceCountArray(dfobj.name);
					if (lineYdata) {
						lineYdata.lineValue.push(dfobj.value);
					} else {
						lineYdata = {
							lineName : dfobj.name,
							lineValue : [ dfobj.value ]
						};
					yDataValue.value.push(lineYdata);
					}
				}
			}
		}
		return {
			xData : xDataValue,
			yData : yDataValue
		};
};





// var linedata = {
// xData : {
// name : '单位',
// value : [ '超高压公司', '调峰调频公司', '广东电网', '广州电网', '深圳电网', '广西电网', '云南电网',
// '贵州电网', '海南电网', '总调直调电厂 ', '广东中调电厂', '广州中调电厂', '深圳中调电厂',
// '广西中调电厂', '云南中调电厂', '贵州中调电厂', '海南中调电厂' ]
// },
// yData : {
// name : '数量',
// value : [
// {
// lineName : '母线',
// lineValue : [ 10, 12, 20, 2, 23, 24, 25, 3, 2, 4, 6, 4, 9,
// 5, 6, 10, 20 ]
// },
// {
// lineName : '线路',
// lineValue : [ 10, 12, 10, 2, 23, 14, 20, 3, 12, 4, 6, 4, 5,
// 5, 6, 6, 9 ]
// },
// {
// lineName : '变压器',
// lineValue : [ 1, 12, 20, 2, 23, 4, 5, 3, 2, 4, 6, 4, 5, 5,
// 12, 6, 4 ]
// },
// {
// lineName : '发电机',
// lineValue : [ 16, 12, 80, 2, 23, 24, 25, 13, 1, 4, 16, 4,
// 6, 5, 6, 6, 13 ]
// } ]
// }
// };
